<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>Chapter 1 I/O | The Open Quant Live Book</title>
  <meta name="description" content="Chapter 1 I/O | The Open Quant Live Book" />
  <meta name="generator" content="bookdown 0.13 and GitBook 2.6.7" />

  <meta property="og:title" content="Chapter 1 I/O | The Open Quant Live Book" />
  <meta property="og:type" content="book" />
  
  <meta property="og:image" content="./fig/cover.jpg" />
  <meta property="og:description" content="Chapter 1 I/O | The Open Quant Live Book" />
  <meta name="github-repo" content="souzatharsis/open-quant-live-book" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Chapter 1 I/O | The Open Quant Live Book" />
  
  <meta name="twitter:description" content="Chapter 1 I/O | The Open Quant Live Book" />
  <meta name="twitter:image" content="./fig/cover.jpg" />

<meta name="author" content="OpenQuants.com" />


<meta name="date" content="2019-12-16" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="index.html"/>
<link rel="next" href="stylized-facts.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />







<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-75703863-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-75703863-2');
</script>


<!-- Hypotheses -->
<!-- <script src="https://hypothes.is/embed.js" async></script> -->


<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>

<link rel="stylesheet" href="custom_style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">The Open Quant Live Book</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Preface</a><ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#description"><i class="fa fa-check"></i>Description</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#contribute"><i class="fa fa-check"></i>Contribute</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#working-contents"><i class="fa fa-check"></i>Working Contents</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#books-information"><i class="fa fa-check"></i>Book’s information</a></li>
</ul></li>
<li class="part"><span><b>I The Basics</b></span></li>
<li class="chapter" data-level="1" data-path="io.html"><a href="io.html"><i class="fa fa-check"></i><b>1</b> I/O</a><ul>
<li class="chapter" data-level="1.1" data-path="io.html"><a href="io.html#importing-data"><i class="fa fa-check"></i><b>1.1</b> Importing Data</a><ul>
<li class="chapter" data-level="1.1.1" data-path="io.html"><a href="io.html#text-files"><i class="fa fa-check"></i><b>1.1.1</b> Text Files</a></li>
<li class="chapter" data-level="1.1.2" data-path="io.html"><a href="io.html#excel-files"><i class="fa fa-check"></i><b>1.1.2</b> Excel Files</a></li>
<li class="chapter" data-level="1.1.3" data-path="io.html"><a href="io.html#json-files"><i class="fa fa-check"></i><b>1.1.3</b> JSON Files</a></li>
<li class="chapter" data-level="1.1.4" data-path="io.html"><a href="io.html#large-files"><i class="fa fa-check"></i><b>1.1.4</b> Large Files</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="io.html"><a href="io.html#data-sources"><i class="fa fa-check"></i><b>1.2</b> Data Sources</a><ul>
<li class="chapter" data-level="1.2.1" data-path="io.html"><a href="io.html#alpha-vantage"><i class="fa fa-check"></i><b>1.2.1</b> Alpha Vantage</a></li>
<li class="chapter" data-level="1.2.2" data-path="io.html"><a href="io.html#iex"><i class="fa fa-check"></i><b>1.2.2</b> IEX</a></li>
<li class="chapter" data-level="1.2.3" data-path="io.html"><a href="io.html#quandl"><i class="fa fa-check"></i><b>1.2.3</b> Quandl</a></li>
<li class="chapter" data-level="1.2.4" data-path="io.html"><a href="io.html#sec"><i class="fa fa-check"></i><b>1.2.4</b> SEC</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="io.html"><a href="io.html#conclusion"><i class="fa fa-check"></i><b>1.3</b> Conclusion</a><ul>
<li class="chapter" data-level="1.3.1" data-path="io.html"><a href="io.html#further-reading"><i class="fa fa-check"></i><b>1.3.1</b> Further Reading</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="stylized-facts.html"><a href="stylized-facts.html"><i class="fa fa-check"></i><b>2</b> Stylized Facts</a><ul>
<li class="chapter" data-level="2.1" data-path="stylized-facts.html"><a href="stylized-facts.html#introduction"><i class="fa fa-check"></i><b>2.1</b> Introduction</a></li>
<li class="chapter" data-level="2.2" data-path="stylized-facts.html"><a href="stylized-facts.html#distribution-of-returns"><i class="fa fa-check"></i><b>2.2</b> Distribution of Returns</a><ul>
<li class="chapter" data-level="2.2.1" data-path="stylized-facts.html"><a href="stylized-facts.html#fat-tails"><i class="fa fa-check"></i><b>2.2.1</b> Fat Tails</a></li>
<li class="chapter" data-level="2.2.2" data-path="stylized-facts.html"><a href="stylized-facts.html#skewness"><i class="fa fa-check"></i><b>2.2.2</b> Skewness</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="stylized-facts.html"><a href="stylized-facts.html#volatility"><i class="fa fa-check"></i><b>2.3</b> Volatility</a><ul>
<li class="chapter" data-level="2.3.1" data-path="stylized-facts.html"><a href="stylized-facts.html#time-invariance"><i class="fa fa-check"></i><b>2.3.1</b> Time-invariance</a></li>
<li class="chapter" data-level="2.3.2" data-path="stylized-facts.html"><a href="stylized-facts.html#volatility-clustering"><i class="fa fa-check"></i><b>2.3.2</b> Volatility Clustering</a></li>
<li class="chapter" data-level="2.3.3" data-path="stylized-facts.html"><a href="stylized-facts.html#correlation-with-trading-volume"><i class="fa fa-check"></i><b>2.3.3</b> Correlation with Trading Volume</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="stylized-facts.html"><a href="stylized-facts.html#correlation"><i class="fa fa-check"></i><b>2.4</b> Correlation</a><ul>
<li class="chapter" data-level="2.4.1" data-path="stylized-facts.html"><a href="stylized-facts.html#time-invariance-1"><i class="fa fa-check"></i><b>2.4.1</b> Time-invariance</a></li>
<li class="chapter" data-level="2.4.2" data-path="stylized-facts.html"><a href="stylized-facts.html#auto-correlation"><i class="fa fa-check"></i><b>2.4.2</b> Auto-correlation</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Algo Trading</b></span></li>
<li class="part"><span><b>III Portfolio Optimization</b></span></li>
<li class="chapter" data-level="3" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html"><i class="fa fa-check"></i><b>3</b> Risk Parity Portfolios</a><ul>
<li class="chapter" data-level="3.1" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#introduction-1"><i class="fa fa-check"></i><b>3.1</b> Introduction</a></li>
<li class="chapter" data-level="3.2" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#risk-parity-portfolio"><i class="fa fa-check"></i><b>3.2</b> Risk Parity Portfolio</a></li>
<li class="chapter" data-level="3.3" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#tangency-portfolio"><i class="fa fa-check"></i><b>3.3</b> Tangency Portfolio</a></li>
<li class="chapter" data-level="3.4" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#optimizing-faang-ray-dalio-versus-markowitz"><i class="fa fa-check"></i><b>3.4</b> Optimizing FAANG: Ray Dalio versus Markowitz</a><ul>
<li class="chapter" data-level="3.4.1" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#single-portfolio"><i class="fa fa-check"></i><b>3.4.1</b> Single Portfolio</a></li>
<li class="chapter" data-level="3.4.2" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#the-ray-dalio-faang-index"><i class="fa fa-check"></i><b>3.4.2</b> The Ray Dalio FAANG Index</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="risk-parity-portfolios.html"><a href="risk-parity-portfolios.html#discussion-and-conclusion"><i class="fa fa-check"></i><b>3.5</b> Discussion and Conclusion</a></li>
</ul></li>
<li class="part"><span><b>IV Machine Learning</b></span></li>
<li class="part"><span><b>V Econophysics</b></span></li>
<li class="chapter" data-level="4" data-path="entropy.html"><a href="entropy.html"><i class="fa fa-check"></i><b>4</b> Entropy</a><ul>
<li class="chapter" data-level="4.1" data-path="entropy.html"><a href="entropy.html#introduction-2"><i class="fa fa-check"></i><b>4.1</b> Introduction</a></li>
<li class="chapter" data-level="4.2" data-path="entropy.html"><a href="entropy.html#nonlinear-coupling"><i class="fa fa-check"></i><b>4.2</b> Nonlinear Coupling</a><ul>
<li class="chapter" data-level="4.2.1" data-path="entropy.html"><a href="entropy.html#simulated-systems"><i class="fa fa-check"></i><b>4.2.1</b> Simulated Systems</a></li>
<li class="chapter" data-level="4.2.2" data-path="entropy.html"><a href="entropy.html#equity-commodities-relationship"><i class="fa fa-check"></i><b>4.2.2</b> Equity-Commodities Relationship</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="entropy.html"><a href="entropy.html#efficiency-and-bubbles-a-case-study-in-the-crypto-and-equity-markets"><i class="fa fa-check"></i><b>4.3</b> Efficiency and Bubbles: A Case Study in the Crypto and Equity Markets</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><i class="fa fa-check"></i><b>5</b> How to Measure Statistical Causality: A Transfer Entropy Approach with Financial Applications</a><ul>
<li class="chapter" data-level="5.1" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#LinearG"><i class="fa fa-check"></i><b>5.1</b> A First Definition of Causality</a></li>
<li class="chapter" data-level="5.2" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#a-probabilistic-based-definition"><i class="fa fa-check"></i><b>5.2</b> A Probabilistic-Based Definition</a></li>
<li class="chapter" data-level="5.3" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#nonlinearG"><i class="fa fa-check"></i><b>5.3</b> Transfer Entropy and Statistical Causality</a></li>
<li class="chapter" data-level="5.4" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#net-information-flow"><i class="fa fa-check"></i><b>5.4</b> Net Information Flow</a></li>
<li class="chapter" data-level="5.5" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#the-link-between-granger-causality-and-transfer-entropy"><i class="fa fa-check"></i><b>5.5</b> The Link Between Granger-causality and Transfer Entropy</a></li>
<li class="chapter" data-level="5.6" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#information-flow-on-simulated-systems"><i class="fa fa-check"></i><b>5.6</b> Information Flow on Simulated Systems</a></li>
<li class="chapter" data-level="5.7" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#information-flow-among-international-stock-market-indices"><i class="fa fa-check"></i><b>5.7</b> Information Flow among International Stock Market Indices</a></li>
<li class="chapter" data-level="5.8" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#other-applications"><i class="fa fa-check"></i><b>5.8</b> Other Applications</a><ul>
<li class="chapter" data-level="5.8.1" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#quantifying-information-flow-between-social-media-and-the-stock-market"><i class="fa fa-check"></i><b>5.8.1</b> Quantifying Information Flow Between Social Media and the Stock Market</a></li>
<li class="chapter" data-level="5.8.2" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#detecting-causal-links-between-investor-sentiment-and-cryptocurrency-prices"><i class="fa fa-check"></i><b>5.8.2</b> Detecting Causal Links Between Investor Sentiment and Cryptocurrency Prices</a></li>
</ul></li>
<li class="chapter" data-level="5.9" data-path="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html"><a href="how-to-measure-statistical-causality-a-transfer-entropy-approach-with-financial-applications.html#conclusions"><i class="fa fa-check"></i><b>5.9</b> Conclusions</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="financial-networks.html"><a href="financial-networks.html"><i class="fa fa-check"></i><b>6</b> Financial Networks</a><ul>
<li class="chapter" data-level="6.1" data-path="financial-networks.html"><a href="financial-networks.html#introduction-3"><i class="fa fa-check"></i><b>6.1</b> Introduction</a></li>
<li class="chapter" data-level="6.2" data-path="financial-networks.html"><a href="financial-networks.html#network-construction"><i class="fa fa-check"></i><b>6.2</b> Network Construction</a><ul>
<li class="chapter" data-level="6.2.1" data-path="financial-networks.html"><a href="financial-networks.html#network-filtering-asset-graphs"><i class="fa fa-check"></i><b>6.2.1</b> Network Filtering: Asset Graphs</a></li>
<li class="chapter" data-level="6.2.2" data-path="financial-networks.html"><a href="financial-networks.html#network-filtering-mst"><i class="fa fa-check"></i><b>6.2.2</b> Network Filtering: MST</a></li>
<li class="chapter" data-level="6.2.3" data-path="financial-networks.html"><a href="financial-networks.html#network-filtering-pmfg"><i class="fa fa-check"></i><b>6.2.3</b> Network Filtering: PMFG</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="financial-networks.html"><a href="financial-networks.html#applications"><i class="fa fa-check"></i><b>6.3</b> Applications</a><ul>
<li class="chapter" data-level="6.3.1" data-path="financial-networks.html"><a href="financial-networks.html#industry-taxonomy"><i class="fa fa-check"></i><b>6.3.1</b> Industry Taxonomy</a></li>
<li class="chapter" data-level="6.3.2" data-path="financial-networks.html"><a href="financial-networks.html#portfolio-construction"><i class="fa fa-check"></i><b>6.3.2</b> Portfolio Construction</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>VI Alternative Data</b></span></li>
<li class="chapter" data-level="7" data-path="the-market-the-players-and-the-rules.html"><a href="the-market-the-players-and-the-rules.html"><i class="fa fa-check"></i><b>7</b> The Market, The Players and The Rules</a><ul>
<li class="chapter" data-level="7.1" data-path="the-market-the-players-and-the-rules.html"><a href="the-market-the-players-and-the-rules.html#the-market"><i class="fa fa-check"></i><b>7.1</b> The Market</a></li>
<li class="chapter" data-level="7.2" data-path="the-market-the-players-and-the-rules.html"><a href="the-market-the-players-and-the-rules.html#the-data"><i class="fa fa-check"></i><b>7.2</b> The Data</a></li>
<li class="chapter" data-level="7.3" data-path="the-market-the-players-and-the-rules.html"><a href="the-market-the-players-and-the-rules.html#the-buyers"><i class="fa fa-check"></i><b>7.3</b> The Buyers</a></li>
<li class="chapter" data-level="7.4" data-path="the-market-the-players-and-the-rules.html"><a href="the-market-the-players-and-the-rules.html#conclusion-1"><i class="fa fa-check"></i><b>7.4</b> Conclusion</a></li>
</ul></li>
<li class="appendix"><span><b>Appendix</b></span></li>
<li class="chapter" data-level="A" data-path="statistical-methods.html"><a href="statistical-methods.html"><i class="fa fa-check"></i><b>A</b> Statistical Methods</a><ul>
<li class="chapter" data-level="A.1" data-path="statistical-methods.html"><a href="statistical-methods.html#kde"><i class="fa fa-check"></i><b>A.1</b> Kernel Density Estimation</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="datasets.html"><a href="datasets.html"><i class="fa fa-check"></i><b>B</b> Datasets</a><ul>
<li class="chapter" data-level="B.1" data-path="datasets.html"><a href="datasets.html#dt-indices"><i class="fa fa-check"></i><b>B.1</b> Log-Returns of International Stock Market Indices Prices</a><ul>
<li class="chapter" data-level="B.1.1" data-path="datasets.html"><a href="datasets.html#dataset-location"><i class="fa fa-check"></i><b>B.1.1</b> Dataset Location</a></li>
<li class="chapter" data-level="B.1.2" data-path="datasets.html"><a href="datasets.html#dataset-description"><i class="fa fa-check"></i><b>B.1.2</b> Dataset Description</a></li>
<li class="chapter" data-level="B.1.3" data-path="datasets.html"><a href="datasets.html#data-source"><i class="fa fa-check"></i><b>B.1.3</b> Data Source</a></li>
<li class="chapter" data-level="B.1.4" data-path="datasets.html"><a href="datasets.html#code"><i class="fa fa-check"></i><b>B.1.4</b> Code</a></li>
<li class="chapter" data-level="B.1.5" data-path="datasets.html"><a href="datasets.html#dataset-scheme"><i class="fa fa-check"></i><b>B.1.5</b> Dataset Scheme</a></li>
</ul></li>
<li class="chapter" data-level="B.2" data-path="datasets.html"><a href="datasets.html#dt-FAANG"><i class="fa fa-check"></i><b>B.2</b> Log-Returns of FAANG Prices</a><ul>
<li class="chapter" data-level="B.2.1" data-path="datasets.html"><a href="datasets.html#dataset-location-1"><i class="fa fa-check"></i><b>B.2.1</b> Dataset Location</a></li>
<li class="chapter" data-level="B.2.2" data-path="datasets.html"><a href="datasets.html#dataset-description-1"><i class="fa fa-check"></i><b>B.2.2</b> Dataset Description</a></li>
<li class="chapter" data-level="B.2.3" data-path="datasets.html"><a href="datasets.html#data-source-1"><i class="fa fa-check"></i><b>B.2.3</b> Data Source</a></li>
<li class="chapter" data-level="B.2.4" data-path="datasets.html"><a href="datasets.html#code-1"><i class="fa fa-check"></i><b>B.2.4</b> Code</a></li>
<li class="chapter" data-level="B.2.5" data-path="datasets.html"><a href="datasets.html#dataset-scheme-1"><i class="fa fa-check"></i><b>B.2.5</b> Dataset Scheme</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
<li class="divider"></li>
<li><a href="https://creativecommons.org/licenses/by/4.0/" target="blank">CC-BY 4.0</a></li>
<li><a href="https://github.com/souzatharsis/open-quant-live-book" target="blank">See source code on Github</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">The Open Quant Live Book</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="io" class="section level1">
<h1><span class="header-section-number">Chapter 1</span> I/O</h1>
<p>In this Chapter, we will introduce basic functions to read text, excel and JSON files as well as large files.</p>
<p>We will also show how to obtain free financial and economic data including the following:</p>
<ul>
<li>End-of-day and real-time pricing;</li>
<li>Company financials;</li>
<li>Macroeconomic data.</li>
</ul>
<p>Data sources utilized in this Chapter include the following:</p>
<ul>
<li>U.S. Securities and Exchange Commission;</li>
<li>Quandl;</li>
<li>IEX;</li>
<li>Alpha Vantage.</li>
</ul>
<div id="importing-data" class="section level2">
<h2><span class="header-section-number">1.1</span> Importing Data</h2>
<div id="text-files" class="section level3">
<h3><span class="header-section-number">1.1.1</span> Text Files</h3>
<p>The most basic and commonly used option to import data from text files in R is the use of the function <code>read.table</code> from the <strong>r-base</strong>. We can use this function to read text files with extensions such as <code>.txt</code> and <code>.csv</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dat.table &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="dt">file =</span> <span class="st">&quot;&lt;name of your file&gt;.txt&quot;</span>)
dat.csv &lt;-<span class="st"> </span><span class="kw">read.csv</span>(<span class="dt">file =</span> <span class="st">&quot;&lt;name of your file&gt;.csv&quot;</span>)</code></pre></div>
<p>The package <strong>readr</strong> provides functions for reading text data into R that are much faster that the functions from the <strong>r-base</strong>. The <code>read_table</code> function from the package <strong>readr</strong> provides a near-replacement for the <code>read.table</code> function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(readr)
dat.table &lt;-<span class="st"> </span>readr<span class="op">::</span><span class="kw">read_table2</span>(<span class="dt">file =</span> <span class="st">&quot;&lt;name of your file&gt;.txt&quot;</span>)
dat.csv &lt;-<span class="st"> </span>readr<span class="op">::</span><span class="kw">read_csv</span>(<span class="dt">file =</span> <span class="st">&quot;&lt;name of your file&gt;.csv&quot;</span>)</code></pre></div>
<p>Another option to save data is to write it in <code>rds</code> format. Data stored in <code>rds</code> format has the advantage to keep the original data struture and type of the object saved. Also, <code>.rds</code> files are compressed and consume less space than files saved in <code>.csv</code> format. A data.frame object can be saved in <code>rds</code> format and then loaded back as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">write_rds</span>(dat.frame, <span class="dt">path =</span> <span class="st">&quot;&lt;name of your file&gt;.rds&quot;</span>)
dat.frame &lt;-<span class="st"> </span><span class="kw">read_rds</span>(<span class="dt">path =</span> <span class="st">&quot;&lt;name of your file&gt;.rds&quot;</span>)</code></pre></div>
</div>
<div id="excel-files" class="section level3">
<h3><span class="header-section-number">1.1.2</span> Excel Files</h3>
<p>The package <code>readxl</code> has an ease to use interface to functions that load excel documents in R. The functions <code>read_xls</code> and <code>read_xlsx</code> can be used to read excel files as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(readxl)
readxl<span class="op">::</span><span class="kw">read_xls</span>(<span class="dt">path =</span> <span class="st">&quot;&lt;name of your file&gt;.xls&quot;</span>)
readxl<span class="op">::</span><span class="kw">read_xlsx</span>(<span class="dt">path =</span> <span class="st">&quot;&lt;name of your file&gt;.xlsx&quot;</span>)</code></pre></div>
<p>The function <code>read_excel()</code> automatically detects the extension of the input file as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">readxl<span class="op">::</span><span class="kw">read_excel</span>(<span class="st">&quot;&lt;name and extension of your file&gt;&quot;</span>, <span class="dt">sheet =</span> <span class="st">&quot;&lt;sheet name or index&gt;&quot;</span>)</code></pre></div>
<p>In the <code>read_excel</code> function, the <code>sheet</code> argument can receive either the target sheet name or index number, where sheet indexing starts at 1.</p>
<p>The <code>readxl</code> has been oberving increased use compared to other comparable packages such as <strong>gdata</strong> and the <strong>xlsx</strong> due to its relative ease of use and performance. Also, the <code>readxl</code> do not have depency with external code libraries while the packages <strong>gdata</strong> and <strong>xlsx</strong> depend on <code>ActiveState PERL</code> and the <code>Java JDK</code>, respectively.</p>
</div>
<div id="json-files" class="section level3">
<h3><span class="header-section-number">1.1.3</span> JSON Files</h3>
<p>JSON files are particularly used for transmitting data in web applications but also frequently used as a standard data interchange format.</p>
<p>The <code>jsonline</code> package can be used to parse files in JSON format as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(jsonlite)
result_json &lt;-<span class="st"> </span><span class="kw">read_json</span>(<span class="st">&quot;&lt;json file&gt;&quot;</span>)</code></pre></div>
</div>
<div id="large-files" class="section level3">
<h3><span class="header-section-number">1.1.4</span> Large Files</h3>
<p>Fast data manipulation in a short and flexible syntax. <!-- https://stackoverflow.com/questions/1727772/quickly-reading-very-large-tables-as-dataframes --></p>
</div>
</div>
<div id="data-sources" class="section level2">
<h2><span class="header-section-number">1.2</span> Data Sources</h2>
<p>In this section, we will show how to obtain financial and economic data from public sources.</p>
<div id="alpha-vantage" class="section level3">
<h3><span class="header-section-number">1.2.1</span> Alpha Vantage</h3>
<p>Alpha Vantage offers free access to pricing data including:</p>
<ul>
<li>Stock Time Series Data;</li>
<li>Physical and Digital/Crypto Currencies (e.g., Bitcoin);</li>
<li>Technical Indicators and</li>
<li>Sector Performances.</li>
</ul>
<p>The data are available in JSON and CSV formats via REST APIs. The <strong>quantmod</strong> and the <strong>alphavantager</strong> R packages offer a lightweight R interface to the Alpha Vantage API. Daily stock prices can be obtained with the <code>quantmod::getSymbols</code> function as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">getSymbols</span>(<span class="dt">Symbols=</span><span class="st">&#39;AAPL&#39;</span>, <span class="dt">src=</span><span class="st">&quot;av&quot;</span>, <span class="dt">output.size=</span><span class="st">&quot;full&quot;</span>, <span class="dt">adjusted=</span><span class="ot">TRUE</span>, <span class="dt">api.key=</span><span class="st">&#39;your API key&#39;</span>)</code></pre></div>
<p>The output data is stored in an object with the same name as the corresponding symbol, in this example <code>AAPL</code>. The output data looks like the following</p>
<table>
<thead>
<tr class="header">
<th align="right">AAPL.Open</th>
<th align="right">AAPL.High</th>
<th align="right">AAPL.Low</th>
<th align="right">AAPL.Close</th>
<th align="right">AAPL.Volume</th>
<th align="right">AAPL.Adjusted</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">62.8</td>
<td align="right">65.0</td>
<td align="right">62.7</td>
<td align="right">64.8</td>
<td align="right">1.12e+08</td>
<td align="right">2.02</td>
</tr>
<tr class="even">
<td align="right">65.0</td>
<td align="right">65.0</td>
<td align="right">62.0</td>
<td align="right">62.1</td>
<td align="right">8.41e+07</td>
<td align="right">1.93</td>
</tr>
<tr class="odd">
<td align="right">62.6</td>
<td align="right">65.9</td>
<td align="right">62.1</td>
<td align="right">65.2</td>
<td align="right">1.59e+08</td>
<td align="right">2.03</td>
</tr>
<tr class="even">
<td align="right">67.0</td>
<td align="right">68.8</td>
<td align="right">66.0</td>
<td align="right">68.6</td>
<td align="right">1.97e+08</td>
<td align="right">2.14</td>
</tr>
<tr class="odd">
<td align="right">67.6</td>
<td align="right">71.4</td>
<td align="right">66.9</td>
<td align="right">70.6</td>
<td align="right">2.24e+08</td>
<td align="right">2.20</td>
</tr>
<tr class="even">
<td align="right">71.9</td>
<td align="right">75.2</td>
<td align="right">70.5</td>
<td align="right">73.5</td>
<td align="right">4.09e+08</td>
<td align="right">2.29</td>
</tr>
</tbody>
</table>
<p><img src="open-quant-live-book_files/figure-html/unnamed-chunk-16-1.png" width="672" /></p>
<p>We called the <code>quantmod::getSymbols</code> function with the following arguments:</p>
<ul>
<li><code>Symbols='AAPL'</code> defines a character vector specifying the names of each symbol to be loaded, here specified by the symbol of the company Apple Inc.;</li>
<li><code>src=&quot;av&quot;</code> specifies the sourcing method, here defined with the value corresponding to Alpha Vantage;</li>
<li><code>output.size=&quot;full&quot;</code>specified length of the time series returned. The strings <code>compact</code> and <code>full</code> are accepted with the following specifications: <code>compact</code> returns only the latest 100 data points; <code>full</code> returns the full-length time series of up to 20 years of historical data;</li>
<li><code>adjusted=TRUE</code> defines a boolean variable to include a column of closing prices adjusted for dividends and splits;</li>
<li><code>api.key</code> specifies your Alpha Vantage API key.</li>
</ul>
</div>
<div id="iex" class="section level3">
<h3><span class="header-section-number">1.2.2</span> IEX</h3>
<p>The IEX Group operates the Investors Exchange (IEX), a stock exchange for U.S. equities that is built for investors and companies. IEX offers U.S. reference and market data including end-of-day and intraday pricing data. IEX offers an API with “a set of services designed for developers and engineers. It can be used to build high-quality apps and services”. Data sourced from the IEX API is freely available for commercial subject to <a href="https://iextrading.com/api-exhibit-a/">conditions</a> and the use of their API is subject to additional <a href="https://iextrading.com/api-terms/">terms of use</a>.</p>
<p>IEX lists the following github project as an unofficial API for R: <a href="https://github.com/imanuelcostigan/iex" class="uri">https://github.com/imanuelcostigan/iex</a>. We will provide examples on how to obtain intraday pricing data using this package. First, we will use the <strong>devtools</strong> to install the package directly from its github repository as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(devtools)
<span class="kw">install_github</span>(<span class="st">&quot;imanuelcostigan/iex&quot;</span>)</code></pre></div>
<p>The <strong>iex</strong> package provides 4 set of functions as follows:</p>
<ul>
<li><code>last</code>: Provides IEX near real time last sale price, size and time. Last is ideal for developers that need a lightweight stock quote. <a href="https://iextrading.com/developer/docs/#last">IEX API real time API documentation</a>.</li>
<li><code>market</code>: Provides exchange trade volume data in near real time. <a href="https://iextrading.com/developer/#market-market">IEX market API documentation</a>.</li>
<li><code>stats</code>: A set of functions that return trading statistics. <a href="https://iextrading.com/developer/#stats">IEX stats API documentation</a>.</li>
<li><code>tops</code>: Provides IEX’s aggregated bid and offer position in near real time for all securities on IEX’s displayed limit order book. <a href="https://iextrading.com/developer/#tops-tops">IEX API TOPS documentation</a>.</li>
</ul>
<p>For instance, the <code>last</code> function has the following arguments:</p>
<ul>
<li><code>symbols</code>: A vector of tickers (case insensitive). Special characters will be escaped. A list of eligible symbols is <a href="https://iextrading.com/trading/eligible-symbols/">published daily</a> by the IEX. When set to <code>NULL</code> (default) returns values for all symbols.</li>
<li><code>fields</code>: A vector of fields names to return (case sensitive). When set to <code>NULL</code> (default) returns values for all fields.</li>
<li><code>version</code>: The API version number, which is used to define the API URL.</li>
</ul>
<p>We can obtain intraday stock price data with the <code>last</code> function as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dat &lt;-<span class="st"> </span>iex<span class="op">::</span><span class="kw">last</span>(<span class="dt">symbols =</span> <span class="kw">c</span>(<span class="st">&quot;AAPL&quot;</span>), <span class="dt">fields  =</span> <span class="kw">c</span>(<span class="st">&quot;symbol&quot;</span>, <span class="st">&quot;price&quot;</span>, <span class="st">&quot;size&quot;</span>)) </code></pre></div>
<p>The function returns an S3 object of class <code>iex_api</code> which has three accessible fields: <code>path</code> , <code>response</code> and <code>content</code>.</p>
<ul>
<li>The <code>path</code> contains the corresponding IEX API path:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dat<span class="op">$</span>path</code></pre></div>
<pre><code>## [1] &quot;tops/last&quot;</code></pre>
<ul>
<li>The <code>response</code> contains the unparsed IEX API response:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dat<span class="op">$</span>response</code></pre></div>
<pre><code>## Response [https://api.iextrading.com/1.0/tops/last?symbols=AAPL&amp;filter=symbol%2Cprice%2Csize]
##   Date: 2019-08-27 02:04
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 45 B</code></pre>
<ul>
<li>The <code>content</code> contains the parsed content from the API’s response:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dat<span class="op">$</span>content</code></pre></div>
<pre><code>## [[1]]
## [[1]]$symbol
## [1] &quot;AAPL&quot;
## 
## [[1]]$price
## [1] 207
## 
## [[1]]$size
## [1] 100</code></pre>
<p>According to the developer, this package causes R to pause 0.2 seconds after executing an API call to avoid the user being throttled by the IEX API (which enforces a 5 request per second limit). Documentation about the other set of functions can be obtained at <a href="https://github.com/imanuelcostigan/iex/tree/master/man" class="uri">https://github.com/imanuelcostigan/iex/tree/master/man</a>.</p>
</div>
<div id="quandl" class="section level3">
<h3><span class="header-section-number">1.2.3</span> Quandl</h3>
<p><a href="https://www.quandl.com"><strong>Quandl</strong></a> is likely the largest financial and alternative data aggregator/provider today. They leverage relationships with third-party providers to be a one-stop-shop for alternative data and traditional fundamental, pricing and estimates datasets.</p>
<p>Quandl offer an API which usage is free for registered users. You can obtain an API key <a href="https://www.quandl.com/sign-up-modal?defaultModal=showSignUp">here</a>. After signing up, just append your API key to your call like this:</p>
<pre><code>https://www.quandl.com/api/v3/datasets/WIKI/FB/data.csv?api_key=YOURAPIKEYHERE</code></pre>
<p>At Quandl, every dataset is identified by “Quandl code”, which is a unique id. In the above example, you downloaded a dataset with the Quandl code “WIKI/FB”.</p>
<p>Every Quandl code has 2 parts: the database code (“WIKI”) which specifies where the data comes from, and the dataset code (“FB”) which identifies the specific time series you want.</p>
<p>You can find Quandl codes using their <a href="https://www.quandl.com/search">data browser</a>. Additional API documentation can be found <a href="https://docs.quandl.com/">here</a>.</p>
<p>Quandl is also available via an R interface <span class="citation">(Raymond McTaggart, Gergely Daroczi, and Clement Leung <a href="#ref-R-Quandl">2019</a>)</span>. For instance, we can obtain Crude Oil Futures prices from 01/01/2010 to 01/01/2019 as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(Quandl)
<span class="kw">Quandl.api_key</span>(config<span class="op">::</span><span class="kw">get</span>()<span class="op">$</span>quandl.key)
from.dat &lt;-<span class="st"> </span><span class="kw">as.Date</span>(<span class="st">&quot;01/01/2010&quot;</span>, <span class="dt">format=</span><span class="st">&quot;%d/%m/%Y&quot;</span>)
to.dat &lt;-<span class="st"> </span><span class="kw">as.Date</span>(<span class="st">&quot;01/01/2019&quot;</span>, <span class="dt">format=</span><span class="st">&quot;%d/%m/%Y&quot;</span>)
crude.oil.futures&lt;-<span class="kw">Quandl</span>(<span class="st">&quot;CHRIS/CME_CL1&quot;</span>, <span class="dt">start_date =</span> from.dat, <span class="dt">end_date =</span> to.dat, <span class="dt">type=</span><span class="st">&quot;xts&quot;</span>)
<span class="kw">plot</span>(crude.oil.futures<span class="op">$</span>Last)</code></pre></div>
<p><img src="open-quant-live-book_files/figure-html/unnamed-chunk-22-1.png" width="672" /> In the example above we specified the following Database/Dataset:</p>
<ul>
<li>Database: “CHRIS”. Continuous contracts for all 600 futures on Quandl. Built on top of raw data from CME, ICE, LIFFE etc. Curated by the Quandl community. 50 years history.</li>
<li>Dataset: “CME_CL1”. Historical futures prices of Crude Oil Futures, Continuous Contract #1. Non-adjusted price based on spot-month continuous contract calculations. Raw data from CME.</li>
</ul>
</div>
<div id="sec" class="section level3">
<h3><span class="header-section-number">1.2.4</span> SEC</h3>
<p>Official filings are freely available from the U.S. Securities and Exchange Commission’s EDGAR database. The package <code>finreportr</code> provides an interface in R to facilitate financial analysis from SEC’s 10K and 10K/A filings.</p>
<p>We can obtain company basic information with the function the <code>CompanyInfo</code> function by passing the ticker symbol of the target company as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">&quot;finreportr&quot;</span>)
AAPL.Info&lt;-<span class="kw">CompanyInfo</span>(<span class="st">&quot;AAPL&quot;</span>)
<span class="kw">print</span>(AAPL.Info)</code></pre></div>
<pre><code>##      company        CIK  SIC state state.inc FY.end     street.address
## 1 Apple Inc. 0000320193 3571    CA        CA   0930 ONE APPLE PARK WAY
##           city.state
## 1 CUPERTINO CA 95014</code></pre>
<p>As a result, we obtain the following information:</p>
<ul>
<li>Company name: Apple Inc.;</li>
<li>SEC Central Index Key (CIK): 0000320193;</li>
<li>Standard Industrial Classification (SIC): 3571, which is the industry code for Electronic Computers;</li>
<li>Address: ONE APPLE PARK WAY, CUPERTINO CA 95014;</li>
<li>Most recent period of report end is 0930.</li>
</ul>
<p>The list of company annual reports with corresponding filing dates can be obtained with the function <em>AnnualReports</em> as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">AAPL.reports&lt;-<span class="kw">AnnualReports</span>(<span class="st">&quot;AAPL&quot;</span>)</code></pre></div>
<table>
<caption><span id="tab:unnamed-chunk-25">Table 1.1: </span>Sample Annual Reports</caption>
<thead>
<tr class="header">
<th align="left">filing.name</th>
<th align="left">filing.date</th>
<th align="left">accession.no</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">10-K</td>
<td align="left">2018-11-05</td>
<td align="left">0000320193-18-000145</td>
</tr>
<tr class="even">
<td align="left">10-K</td>
<td align="left">2017-11-03</td>
<td align="left">0000320193-17-000070</td>
</tr>
<tr class="odd">
<td align="left">10-K</td>
<td align="left">2016-10-26</td>
<td align="left">0001628280-16-020309</td>
</tr>
<tr class="even">
<td align="left">10-K</td>
<td align="left">2015-10-28</td>
<td align="left">0001193125-15-356351</td>
</tr>
<tr class="odd">
<td align="left">10-K</td>
<td align="left">2014-10-27</td>
<td align="left">0001193125-14-383437</td>
</tr>
<tr class="even">
<td align="left">10-K</td>
<td align="left">2013-10-30</td>
<td align="left">0001193125-13-416534</td>
</tr>
</tbody>
</table>
<p>The accession number is a unique identifier that the SEC creates for each filing.</p>
<p>Company financials are organized into 3 segments: Income Statement, Balance Sheet and Cash Flow.</p>
<p><strong>Income Statement</strong></p>
<p>Financials from the Income Statement segment can be obtained with the <em>GetIncome</em> function as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">AAPL.IS&lt;-<span class="kw">GetIncome</span>(<span class="st">&quot;AAPL&quot;</span>, <span class="dv">2017</span>)</code></pre></div>
<!-- I have pre-cached this object as sometimes the above call can timeout if the EDGAR database is too slow. -->
<!-- Be aware of that and include a try-catch clause for robustness. -->
<table>
<caption><span id="tab:unnamed-chunk-28">Table 1.2: </span>Sample Income Statement Financials</caption>
<thead>
<tr class="header">
<th align="left">Metric</th>
<th align="left">Units</th>
<th align="left">Amount</th>
<th align="left">startDate</th>
<th align="left">endDate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">233715000000</td>
<td align="left">2014-09-28</td>
<td align="left">2015-09-26</td>
</tr>
<tr class="even">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">75872000000</td>
<td align="left">2015-09-27</td>
<td align="left">2015-12-26</td>
</tr>
<tr class="odd">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">50557000000</td>
<td align="left">2015-12-27</td>
<td align="left">2016-03-26</td>
</tr>
<tr class="even">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">42358000000</td>
<td align="left">2016-03-27</td>
<td align="left">2016-06-25</td>
</tr>
<tr class="odd">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">46852000000</td>
<td align="left">2016-06-26</td>
<td align="left">2016-09-24</td>
</tr>
<tr class="even">
<td align="left">Revenue, Net</td>
<td align="left">usd</td>
<td align="left">215639000000</td>
<td align="left">2015-09-27</td>
<td align="left">2016-09-24</td>
</tr>
</tbody>
</table>
<p>The Income Statement function returns data for the following metrics:</p>
<table>
<caption><span id="tab:unnamed-chunk-29">Table 1.3: </span>Income Statement Metrics</caption>
<colgroup>
<col width="100%" />
</colgroup>
<thead>
<tr class="header">
<th align="left">Metrics</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Revenue, Net</td>
</tr>
<tr class="even">
<td align="left">Cost of Goods and Services Sold</td>
</tr>
<tr class="odd">
<td align="left">Gross Profit</td>
</tr>
<tr class="even">
<td align="left">Research and Development Expense</td>
</tr>
<tr class="odd">
<td align="left">Selling, General and Administrative Expense</td>
</tr>
<tr class="even">
<td align="left">Operating Expenses</td>
</tr>
<tr class="odd">
<td align="left">Operating Income (Loss)</td>
</tr>
<tr class="even">
<td align="left">Nonoperating Income (Expense)</td>
</tr>
<tr class="odd">
<td align="left">Income (Loss) from Continuing Operations before Income Taxes, Noncontrolling Interest</td>
</tr>
<tr class="even">
<td align="left">Income Tax Expense (Benefit)</td>
</tr>
<tr class="odd">
<td align="left">Net Income (Loss) Attributable to Parent</td>
</tr>
<tr class="even">
<td align="left">Earnings Per Share, Basic</td>
</tr>
<tr class="odd">
<td align="left">Earnings Per Share, Diluted</td>
</tr>
<tr class="even">
<td align="left">Weighted Average Number of Shares Outstanding, Basic</td>
</tr>
<tr class="odd">
<td align="left">Weighted Average Number of Shares Outstanding, Diluted</td>
</tr>
<tr class="even">
<td align="left">Common Stock, Dividends, Per Share, Declared</td>
</tr>
</tbody>
</table>
<p><strong>Balance Sheet</strong></p>
<p>Financials from the Balance Sheet segment can be obtained with the <em>GetBalanceSheet</em> function as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">AAPL.BS&lt;-<span class="kw">GetBalanceSheet</span>(<span class="st">&quot;AAPL&quot;</span>, <span class="dv">2017</span>)</code></pre></div>
<!-- I have pre-cached this object as sometimes the above call can timeout if the EDGAR database is too slow. -->
<!-- Be aware of that and include a try-catch clause for robustness. -->
<table>
<caption><span id="tab:unnamed-chunk-32">Table 1.4: </span>Sample Balance Sheet Financials</caption>
<thead>
<tr class="header">
<th align="left">Metric</th>
<th align="left">Units</th>
<th align="left">Amount</th>
<th align="left">startDate</th>
<th align="left">endDate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">13844000000</td>
<td align="left">NA</td>
<td align="left">2014-09-27</td>
</tr>
<tr class="even">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">21120000000</td>
<td align="left">NA</td>
<td align="left">2015-09-26</td>
</tr>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">20484000000</td>
<td align="left">NA</td>
<td align="left">2016-09-24</td>
</tr>
<tr class="even">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">20289000000</td>
<td align="left">NA</td>
<td align="left">2017-09-30</td>
</tr>
<tr class="odd">
<td align="left">Available-for-sale Securities, Current</td>
<td align="left">usd</td>
<td align="left">46671000000</td>
<td align="left">NA</td>
<td align="left">2016-09-24</td>
</tr>
<tr class="even">
<td align="left">Available-for-sale Securities, Current</td>
<td align="left">usd</td>
<td align="left">53892000000</td>
<td align="left">NA</td>
<td align="left">2017-09-30</td>
</tr>
</tbody>
</table>
<p>The Balance Sheet function returns data for the following metrics:</p>
<table>
<caption><span id="tab:unnamed-chunk-33">Table 1.5: </span>Balance Sheet Metrics</caption>
<thead>
<tr class="header">
<th align="left">Metrics</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
</tr>
<tr class="even">
<td align="left">Available-for-sale Securities, Current</td>
</tr>
<tr class="odd">
<td align="left">Accounts Receivable, Net, Current</td>
</tr>
<tr class="even">
<td align="left">Inventory, Net</td>
</tr>
<tr class="odd">
<td align="left">Nontrade Receivables, Current</td>
</tr>
<tr class="even">
<td align="left">Other Assets, Current</td>
</tr>
<tr class="odd">
<td align="left">Assets, Current</td>
</tr>
<tr class="even">
<td align="left">Available-for-sale Securities, Noncurrent</td>
</tr>
<tr class="odd">
<td align="left">Property, Plant and Equipment, Net</td>
</tr>
<tr class="even">
<td align="left">Goodwill</td>
</tr>
<tr class="odd">
<td align="left">Intangible Assets, Net (Excluding Goodwill)</td>
</tr>
<tr class="even">
<td align="left">Other Assets, Noncurrent</td>
</tr>
<tr class="odd">
<td align="left">Assets</td>
</tr>
<tr class="even">
<td align="left">Accounts Payable, Current</td>
</tr>
<tr class="odd">
<td align="left">Accrued Liabilities, Current</td>
</tr>
<tr class="even">
<td align="left">Deferred Revenue, Current</td>
</tr>
<tr class="odd">
<td align="left">Commercial Paper</td>
</tr>
<tr class="even">
<td align="left">Long-term Debt, Current Maturities</td>
</tr>
<tr class="odd">
<td align="left">Liabilities, Current</td>
</tr>
<tr class="even">
<td align="left">Deferred Revenue, Noncurrent</td>
</tr>
<tr class="odd">
<td align="left">Long-term Debt, Excluding Current Maturities</td>
</tr>
<tr class="even">
<td align="left">Other Liabilities, Noncurrent</td>
</tr>
<tr class="odd">
<td align="left">Liabilities</td>
</tr>
<tr class="even">
<td align="left">Commitments and Contingencies</td>
</tr>
<tr class="odd">
<td align="left">Common Stocks, Including Additional Paid in Capital</td>
</tr>
<tr class="even">
<td align="left">Retained Earnings (Accumulated Deficit)</td>
</tr>
<tr class="odd">
<td align="left">Accumulated Other Comprehensive Income (Loss), Net of Tax</td>
</tr>
<tr class="even">
<td align="left">Stockholders’ Equity Attributable to Parent</td>
</tr>
<tr class="odd">
<td align="left">Liabilities and Equity</td>
</tr>
</tbody>
</table>
<p><strong>Cash Flow</strong></p>
<p>Financials from the Cash Flow segment can be obtained with the <em>GetCashFlow</em> function as follows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">AAPL.CF&lt;-<span class="kw">GetCashFlow</span>(<span class="st">&quot;AAPL&quot;</span>, <span class="dv">2017</span>)</code></pre></div>
<!-- I have pre-cached this object as sometimes the above call can timeout if the EDGAR database is too slow. -->
<!-- Be aware of that and include a try-catch clause for robustness. -->
<table>
<caption><span id="tab:unnamed-chunk-36">Table 1.6: </span>Sample Cash Flow Financials</caption>
<thead>
<tr class="header">
<th align="left">Metric</th>
<th align="left">Units</th>
<th align="left">Amount</th>
<th align="left">startDate</th>
<th align="left">endDate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">13844000000</td>
<td align="left">NA</td>
<td align="left">2014-09-27</td>
</tr>
<tr class="even">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">21120000000</td>
<td align="left">NA</td>
<td align="left">2015-09-26</td>
</tr>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">20484000000</td>
<td align="left">NA</td>
<td align="left">2016-09-24</td>
</tr>
<tr class="even">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
<td align="left">usd</td>
<td align="left">20289000000</td>
<td align="left">NA</td>
<td align="left">2017-09-30</td>
</tr>
<tr class="odd">
<td align="left">Net Income (Loss) Attributable to Parent</td>
<td align="left">usd</td>
<td align="left">53394000000</td>
<td align="left">2014-09-28</td>
<td align="left">2015-09-26</td>
</tr>
<tr class="even">
<td align="left">Net Income (Loss) Attributable to Parent</td>
<td align="left">usd</td>
<td align="left">18361000000</td>
<td align="left">2015-09-27</td>
<td align="left">2015-12-26</td>
</tr>
</tbody>
</table>
<p>The Cash Flow function returns data for the following metrics:</p>
<table>
<caption><span id="tab:unnamed-chunk-37">Table 1.7: </span>Cash Flow Metrics</caption>
<colgroup>
<col width="100%" />
</colgroup>
<thead>
<tr class="header">
<th align="left">Metrics</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, at Carrying Value</td>
</tr>
<tr class="even">
<td align="left">Net Income (Loss) Attributable to Parent</td>
</tr>
<tr class="odd">
<td align="left">Depreciation, Amortization and Accretion, Net</td>
</tr>
<tr class="even">
<td align="left">Share-based Compensation</td>
</tr>
<tr class="odd">
<td align="left">Deferred Income Tax Expense (Benefit)</td>
</tr>
<tr class="even">
<td align="left">Other Noncash Income (Expense)</td>
</tr>
<tr class="odd">
<td align="left">Increase (Decrease) in Accounts Receivable</td>
</tr>
<tr class="even">
<td align="left">Increase (Decrease) in Inventories</td>
</tr>
<tr class="odd">
<td align="left">Increase (Decrease) in Other Receivables</td>
</tr>
<tr class="even">
<td align="left">Increase (Decrease) in Other Operating Assets</td>
</tr>
<tr class="odd">
<td align="left">Increase (Decrease) in Accounts Payable</td>
</tr>
<tr class="even">
<td align="left">Increase (Decrease) in Deferred Revenue</td>
</tr>
<tr class="odd">
<td align="left">Increase (Decrease) in Other Operating Liabilities</td>
</tr>
<tr class="even">
<td align="left">Net Cash Provided by (Used in) Operating Activities</td>
</tr>
<tr class="odd">
<td align="left">Payments to Acquire Available-for-sale Securities</td>
</tr>
<tr class="even">
<td align="left">Proceeds from Maturities, Prepayments and Calls of Available-for-sale Securities</td>
</tr>
<tr class="odd">
<td align="left">Proceeds from Sale of Available-for-sale Securities</td>
</tr>
<tr class="even">
<td align="left">Payments to Acquire Businesses, Net of Cash Acquired</td>
</tr>
<tr class="odd">
<td align="left">Payments to Acquire Property, Plant, and Equipment</td>
</tr>
<tr class="even">
<td align="left">Payments to Acquire Intangible Assets</td>
</tr>
<tr class="odd">
<td align="left">Payments to Acquire Other Investments</td>
</tr>
<tr class="even">
<td align="left">Payments for (Proceeds from) Other Investing Activities</td>
</tr>
<tr class="odd">
<td align="left">Net Cash Provided by (Used in) Investing Activities</td>
</tr>
<tr class="even">
<td align="left">Proceeds from Issuance of Common Stock</td>
</tr>
<tr class="odd">
<td align="left">Excess Tax Benefit from Share-based Compensation, Financing Activities</td>
</tr>
<tr class="even">
<td align="left">Payments Related to Tax Withholding for Share-based Compensation</td>
</tr>
<tr class="odd">
<td align="left">Payments of Dividends</td>
</tr>
<tr class="even">
<td align="left">Payments for Repurchase of Common Stock</td>
</tr>
<tr class="odd">
<td align="left">Proceeds from Issuance of Long-term Debt</td>
</tr>
<tr class="even">
<td align="left">Repayments of Long-term Debt</td>
</tr>
<tr class="odd">
<td align="left">Proceeds from (Repayments of) Commercial Paper</td>
</tr>
<tr class="even">
<td align="left">Net Cash Provided by (Used in) Financing Activities</td>
</tr>
<tr class="odd">
<td align="left">Cash and Cash Equivalents, Period Increase (Decrease)</td>
</tr>
<tr class="even">
<td align="left">Income Taxes Paid, Net</td>
</tr>
<tr class="odd">
<td align="left">Interest Paid</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="conclusion" class="section level2">
<h2><span class="header-section-number">1.3</span> Conclusion</h2>
<ul>
<li>We showed how to load and import data from both local files and external sources.</li>
<li>We provided examples on how to read tabular data and how to handle large files.</li>
<li>We showed how to obtain financial and economic data from freely available sources.</li>
</ul>
<div id="further-reading" class="section level3">
<h3><span class="header-section-number">1.3.1</span> Further Reading</h3>
<p>To further learn how to use R to load, transform, visualize and model data see <span class="citation">(Wickham and Grolemund <a href="#ref-Wickham:2017:RDS:3086927">2017</a>)</span>. Additional relevant R packages include the following:</p>
<ul>
<li>dplyr: Fast data frames manipulation and database query.</li>
<li>reshape2: Flexibly rearrange, reshape and aggregate data.</li>
<li>readr: A fast and friendly way to read tabular data into R.</li>
<li>tidyr: Easily tidy data with spread and gather functions.</li>
<li>rlist: A toolbox for non-tabular data manipulation with lists.</li>
<li>jsonlite: A robust and quick way to parse JSON files in R.</li>
<li>ff: Data structures designed to store large datasets.</li>
<li>lubridate: A set of functions to work with dates and times.</li>
</ul>

</div>
</div>
</div>
<h3>References</h3>
<div id="refs" class="references">
<div id="ref-R-Quandl">
<p>Raymond McTaggart, Gergely Daroczi, and Clement Leung. 2019. <em>Quandl: API Wrapper for Quandl.com</em>. <a href="https://CRAN.R-project.org/package=Quandl" class="uri">https://CRAN.R-project.org/package=Quandl</a>.</p>
</div>
<div id="ref-Wickham:2017:RDS:3086927">
<p>Wickham, Hadley, and Garrett Grolemund. 2017. <em>R for Data Science: Import, Tidy, Transform, Visualize, and Model Data</em>. 1st ed. O’Reilly Media, Inc.</p>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="index.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="stylized-facts.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"linkedin": true,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/souzatharsis/open-quant-live-book/edit/master/chapters/IO/IO.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"download": ["open-quant-live-book.epub"],
"toc": {
"collapse": "subsection",
"scroll_highlight": true
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
